package com.htw.rv.fast_scroller.scroller_bind_view

import android.os.Bundle
import android.view.ViewTreeObserver
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.htw.rv.fast_scroller.adapter.RvAdapter
import com.htw.rv.fast_scroller.databinding.ActivityFastScrollerBindBinding
import com.htw.rv.fast_scroller.follow_scroller_bar.FastScrollerBuilder
import com.htw.rv.fast_scroller.follow_scroller_bar.FastScrollerLocationChangeListener
import com.htw.rv.fast_scroller.follow_scroller_bar.FastScrollerState
import com.htw.rv.fast_scroller.follow_scroller_bar.FastScrollerStateChangeListener

/**
 * @author htw
 * createTime: 2022-04-25
 * desc: 将ScrollerBar 替换为 View, 使用时, 请自行再封装
 */
class FastScrollerBindViewActivity : AppCompatActivity() {

    private lateinit var binding: ActivityFastScrollerBindBinding

    private var mRvAdapter: RvAdapter? = null
    private val mFastScrollerBuilder = FastScrollerBuilder()
    private val mFastScrollerBuilderProxy = FastScrollerBuilderProxy()

    private var llScrollBarWH = IntArray(2)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFastScrollerBindBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initOnGlobalLayoutListener()
        initViews()
        initRv()
    }

    private fun initViews() {
        binding.llScrollBar.tvScrollTitle.text = ""
    }

    private fun initOnGlobalLayoutListener() {
        binding.llScrollBar.root.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                llScrollBarWH[0] = binding.llScrollBar.root.width
                llScrollBarWH[1] = binding.llScrollBar.root.height
                binding.llScrollBar.root.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })
    }

    private fun initRv() {
        val adapter = RvAdapter().apply {
            initListData()
        }
        mRvAdapter = adapter
        binding.rvContent.layoutManager = LinearLayoutManager(this)
        binding.rvContent.adapter = adapter

        initFastScroller()
    }

    private fun initFastScroller() {
        initFastScrollerBuilder()
        initFastScrollerDrawables(binding.rvContent)
        //fastScrollerTransY()
    }

    private fun initFastScrollerBuilder() {
        mFastScrollerBuilder.setFastScrollerStateChangeListener(object :
            FastScrollerStateChangeListener {
            override fun onFastScrollerStateChange(oldState: Int, state: Int) {
                mFastScrollerBuilder.setFastScrollerState(state)
                when (mFastScrollerBuilder.getFastScrollerState()) {
                    FastScrollerState.Visible -> {
                        binding.llScrollBar.root.isVisible = true
                    }
                    FastScrollerState.Hidden -> {
                        binding.llScrollBar.root.isVisible = false
                    }
                    FastScrollerState.Dragging -> {
                        binding.llScrollBar.root.isVisible = true
                    }
                }
            }
        })
        mFastScrollerBuilder.setFastScrollerLocationChangeListener(object :
            FastScrollerLocationChangeListener {
            override fun verticalLocationChange(y: Float) {
                val layoutParams =
                    binding.llScrollBar.root.layoutParams as ConstraintLayout.LayoutParams
                layoutParams.topMargin = (y - llScrollBarWH[1].toFloat() / 2.0F).toInt()
                binding.llScrollBar.root.layoutParams = layoutParams

                val firstVisibleItemPosition =
                    (binding.rvContent.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
                binding.llScrollBar.tvScrollTitle.text =
                    mRvAdapter?.getItemData(firstVisibleItemPosition)?.name ?: ""
            }

        })

        mFastScrollerBuilderProxy.setFastScrollerBuilder(mFastScrollerBuilder)
        mFastScrollerBuilderProxy.setFastScrollerBindViewWH(object : FastScrollerBindViewWH {
            override fun getWidth(): Int {
                return llScrollBarWH[0]
            }

            override fun getHeight(): Int {
                return llScrollBarWH[1]
            }

        })
        mFastScrollerBuilderProxy.setFastScrollerAlphaListener(object : FastScrollerAlphaListener {
            override fun onAlphaChange(alpha: Int) {
                if (alpha == 0) {
                    binding.llScrollBar.root.isVisible = false
                } else if (alpha == 255) {
                    binding.llScrollBar.root.isVisible = true
                }
            }
        })
    }

    private fun initFastScrollerDrawables(recyclerView: RecyclerView) {
        bindFastScroller(recyclerView)
    }

    private fun bindFastScroller(
        recyclerView: RecyclerView,
    ) {

        FastScrollerBindView(
            mFastScrollerBuilderProxy,
            recyclerView,
            resources.getDimensionPixelSize(androidx.recyclerview.R.dimen.fastscroll_minimum_range),
            resources.getDimensionPixelOffset(androidx.recyclerview.R.dimen.fastscroll_margin)
        )
    }

}